package order

import (
	"data_summary/common/db"
	models "data_summary/common/models/order"
	"data_summary/common/models/stat"
	"gorm.io/gorm"
)

type OrderProDao struct {
	db *gorm.DB
}

func NewOrderPro(db *gorm.DB) *OrderProDao {
	dao := &OrderProDao{
		db: db,
	}
	return dao
}

func (c *OrderProDao) TableName(suffix string) *OrderProDao {
	c.db = c.db.Table("wwj_order_pro_" + suffix)
	return c
}

// 添加
func (c *OrderProDao) Create(v *models.OrderPro) (id uint, err error) {
	err = c.db.Create(&v).Error
	return v.Id, err
}

// 修改
func (c *OrderProDao) Update(v *models.OrderPro) (id uint, err error) {
	err = c.db.Save(&v).Error
	return v.Id, err
}

// 删除
func (c *OrderProDao) Delete(v *models.OrderPro) (id uint, err error) {
	err = c.db.Delete(&v).Error
	return v.Id, err
}

func (c *OrderProDao) Get(where map[string]interface{}, order string, pageSize int, page int) interface{} {
	var v []models.OrderPro
	c.db.Where(where).Order(order).Limit(pageSize).Offset(page).Find(&v)
	return v
}

// 根据group 获取统计数据
func (c *OrderProDao) GetTotal(where map[string]interface{}, filed string, group string) (v []stat.StatDayOrder) {
	cond, values, _ := db.WhereBuild(where)
	c.db.Where(cond, values...).Select(filed).Group(group).Find(&v)
	//fmt.Print("打印数据---", v)
	return v
}
